package org.example.sunrise.views;

import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.application.Platform;
import org.example.sunrise.views.models.EmploymentRequestViewModel;

public class EmploymentRequestView extends VBox {
    private final TextField tfName = new TextField();
    private final TextField tfPosition = new TextField();
    private final TextField tfAnnualSalary = new TextField();
    private final EmploymentRequestViewModel viewModel = new EmploymentRequestViewModel();

    public EmploymentRequestView() {
        createView();
        bindViewModel();
    }

    private void createView() {
        VBox groupWrap = new VBox();
        groupWrap.setAlignment(Pos.CENTER);
        groupWrap.getChildren().add(createPane());
        VBox.setVgrow(groupWrap, Priority.ALWAYS);
        this.getChildren().addAll(groupWrap, new Separator(), createBar());
    }

    private GridPane createPane() {
        GridPane gp = new GridPane();
        gp.setPadding(new Insets(40.0d));
        gp.setVgap(4);

        gp.add(new Label("Name"), 0, 0);
        gp.add(tfName, 1, 0);

        gp.add(new Label("Desired Position"), 0, 1);
        gp.add(tfPosition, 1, 1);

        gp.add(new Label("Current Annual Salary"), 0, 2);
        gp.add(tfAnnualSalary, 1, 2);

        ColumnConstraints col = new ColumnConstraints();
        col.setPercentWidth(50);
        gp.getColumnConstraints().addAll(col, col);
        return gp;
    }

    private ButtonBar createBar() {
        Button btnSave = new Button("Save");
        Button btnCancel = new Button("Cancel");
        Button btnReset = new Button("Reset");

        btnSave.setOnAction(this::save);
        btnCancel.setOnAction(this::cancel);
        btnReset.setOnAction(this::reset);

        ButtonBar buttonBar = new ButtonBar();
        buttonBar.setPadding(new Insets(20.0d));
        ButtonBar.setButtonData(btnSave, ButtonBar.ButtonData.OK_DONE);
        ButtonBar.setButtonData(btnCancel, ButtonBar.ButtonData.CANCEL_CLOSE);
        ButtonBar.setButtonData(btnReset, ButtonBar.ButtonData.APPLY);

        buttonBar.getButtons().addAll(btnSave, btnCancel, btnReset);
        return buttonBar;
    }

    private void bindViewModel() {
        viewModel.bindNameProperty(tfName.textProperty());
        viewModel.bindPositionProperty(tfPosition.textProperty());
        viewModel.bindAnnualSalaryProperty(tfAnnualSalary.textProperty());
    }

    private void save(ActionEvent evt) {
        viewModel.save();
    }

    private void cancel(ActionEvent evt) {
        Platform.exit();
    }

    private void reset(ActionEvent evt) {
        viewModel.reset();
    }
}
